package com.eveinfo.display.certificates;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.api.utils.certificates.ApiCategory;
import com.eveinfo.api.utils.certificates.ApiClass;
import com.eveinfo.tools.ActivityLauncher;

public class CategoriesAdapter extends BaseAdapter {

	private final List<ApiCategory> categories;
	private final Activity activity;
	private final int characterID;
	private Integer[] unfold;
	private CertificatesFilter filter;

	public CategoriesAdapter(Activity activity, ListView listview, List<ApiCategory> categories,
			CertificatesFilter filter, int characterID) {
		this. characterID = characterID;
		this.activity = activity;
		this.categories = categories;
		this.filter = filter;
		this.unfold = new Integer[categories.size()];
		for (int i = 0; i < categories.size(); i++) {
			unfold[i] = 0;
		}
		listview.setAdapter(this);
	}

	public Object getItem(int position) {
		return null;
	}

	public int getCount() {
		int total = 0;
		int i = 0;
		for (ApiCategory category : categories) {
			if (unfold[i] == 1) {
				total++;
				for (ApiClass c : category.getClasses()) {
					if (filter.isDisplay(c)) {
						total++;
					}
				}
			} else {
				total++;
			}
			i++;
		}
		return total;
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		int sectionnum = 0;
		int pos = position;
		int i = 0;
		for (ApiCategory category : categories) {
			final int y = i;

			int size = 1;
			int realPos = pos - 1;

			ArrayList<ApiClass> sectionClass = category.getClasses();

			if (unfold[i] == 1) {
				int z = pos - 1;
				for (ApiClass c : sectionClass) {
					if (filter.isDisplay(c)) {
						z--;
						size++;
					} else {
						if (z >= 0) {
							realPos++;
						}
					}
				}
			}

			TextView label = new TextView(activity);
			if (pos == 0) {
				label.setPadding(5, 3, 0, 3);
				label.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (unfold[y] == 0) {
							unfold[y] = 1;
						} else {
							unfold[y] = 0;
						}
						notifyDataSetChanged();
					}
				});
				int number = 0;
				for (ApiClass c : sectionClass) {
					if (filter.isDisplay(c)) {
						number++;
					}
				}
				label.setTextSize(20);
				label.setText(category.getName());
				return label;
			}
			if (pos < size) {
				label.setTextSize(15);
				label.setPadding(15, 5, 0, 5);
				label.setText(category.getClass(realPos).getName());
				final int typeID = category.getClass(realPos).getID();
				label.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						ActivityLauncher.displayCertificates(activity, typeID, characterID);
					}
				});
				return label;
			}

			pos -= size;
			sectionnum++;
			i++;
		}

		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setFilter(CertificatesFilter filter) {
		this.filter = filter;
		notifyDataSetChanged();
	}

}
